<?php

function getAlbums() {

    global $db;

    $table = 'albums';

    $db->select($table);
    $albums = $db->fetchAll();

    return $albums;
}

function checkAlbumByName($albumName) {

    global $db;

    $db->select('albums', 'id', ' name = :name', array('name' => $albumName));
    $album_data = $db->fetch();

    if($db->rowCount() == 1) {
        return $album_data['id'];
    }

    return false;
}

function checkImageById($imageId) {

    global $db;

    $db->select('photographs', 'id', ' id = :id', array('id' => $imageId));
    $db->fetch();

    if($db->rowCount() == 1) {
        return true;
    }

    return false;
}

function getDefaultPic() {

    global $db;

    $db->select('albums', 'id', ' 1 ORDER BY id');
    $album_data = $db->fetch();
    $album_id = $album_data['id'];

    $db->select('photographs', '*', 'album_id = :album_id ORDER BY `id`', array('album_id' => $album_id));
    $image = $db->fetch();

    if($db->rowCount() > 0) {
        return $image;
    }

    return false;
}

function getSpecificPic($album, $image = null) {

    global $db;
    $where_sql = '';
    $where_data = array();

    if($album != null) {

        if(checkAlbumByName($album)) {
            $where_sql = 'album_id = :album_id ';
            $where_data['album_id'] = checkAlbumByName($album);
        }
        else {
            // nqma takuv album => error
        }
    }

    if($image != null) {

        if(checkImageById($image)) {
            $where_sql .= 'AND id = :id ';
            $where_data['id'] = $image;
        }
        else {
            // nqma takava snimka = > error
        }
    }


    $db->select('photographs', '*', $where_sql, $where_data);
    $image = $db->fetch();

    if($db->rowCount() > 0) {
        return $image;
    }

    return false;
}

function getAlt($album_id = null) {

    global $db;

    if($album_id == null) {
        return false;
    }

    $db->select('albums', 'alt', 'id = :id', array('id' => $album_id));
    $alt = $db->fetch();

    if($db->rowCount() > 0) {
        return $alt['alt'];
    }

    return false;
}


function getAlbumNameById($album_id = null) {

    global $db;

    if($album_id == null) {
        return false;
    }

    $db->select('albums', 'name', 'id = :id', array('id' => $album_id));
    $album_data = $db->fetch();

    if($db->rowCount() > 0) {
        return $album_data['name'];
    }

    return false;
}





function indexOf($index, $array) {

    $new = array();

    foreach ($array as $arr) {
        $new[$arr[$index]] = $arr;
    }

    return $new;
}

function getPics($album, $image) {

    global $db;


    if($album == null) {

        $image = getDefaultPic();
    }
    else {
        if(checkAlbumByName($album)) {
            $album_id = checkAlbumByName($album);
            $image = getSpecificPic($album_id, $image);
        }
        else {
            $image = getDefaultPic();
        }
    }

    var_dump($image);
    die('s');

    $album_name = getAlbumNameById($image['album_id']);
    $alt = getAlt($image['album_id']);

    var_dump($image);
    echo '<br />';
    var_dump($album_name);
    echo '<br />';
    var_dump($alt);

    die();

    $alt = getAlt($image['album_id']);

    $imagePath = ALBUM_PATH . $albums[$pic['album_id']]['name'] . '/' . $pic['source'];


        ?>

    <table style="margin: 0 auto;">
        <colgroup>
            <col style="width: 170px;" />
            <col style="width: 50px;" />
            <col style="width: 170px;" />
        </colgroup>
        <tr>
            <td>


            </td>
            <td>
                <?php echo $hidden; ?>
                    <header style="height: 20px; overflow: hidden;">
                        <h2 class="f_left"><?php echo $title; ?><h2>
                            <div class="fb-like f_right" style="margin-right: -29px;" data-href="http://www.facebook.com/pages/%D0%97%D0%B0%D0%B1%D0%B0%D0%B2%D0%BB%D1%8F%D0%B2%D0%B0%D0%B9-%D1%81%D0%B5-%D0%B5%D0%B4%D0%B8%D0%BD-%D0%BF%D1%8A%D1%82-%D1%81%D0%B5-%D0%B6%D0%B8%D0%B2%D0%B5%D0%B5/459778490725668" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false"></div>
                            <div class="clear"></div>
                    </header>
                    <table>
                        <tr>
                            <td style="vertical-align: top">
                                <img src="<?php echo $imagePath; ?>" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>" style="max-height: 500px;" class="box"/><br/>
                            </td>
                            <td style="vertical-align: top">

                            </td>
                        </tr>
                    </table>
                    <div class="fb-comments" data-href="<?php echo $imagePath; ?>" data-width="318" data-num-posts="1"></div>




                    <div id="fb-root"></div>
            </td>
            <td style="padding-left: 10px; vertical-align: top;">
                <div class="content-category" style="display: none">
                    <h3>Ime na papka</h3>
                    <!-- Hide on Mouse out -->
                    <div class="content">
                        Lamborghini's Sant'Agata Bolognese production facility produces V12 engines and finished automobiles. Lamborghini's current production vehicles are the V10-powered Gallardo and the V12-powered Aventador. Both production models are available in a variety of regular and limited-edition specifications.
                    </div>
                    <div style="height: 1px; background: #ccc;"></div>
                    <img src="img/300img-logo-smal.png" class="f_left" />
                </div>

            </td>
        </tr>
    </table>



    <?php


}

function deleteDir($dirPath)
{
    if (!is_dir($dirPath)) {
        throw new InvalidArgumentException("$dirPath must be a directory");
    }
    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
        $dirPath .= '/';
    }
    $files = glob($dirPath . '*', GLOB_MARK);
    foreach ($files as $file) {
        if (is_dir($file)) {
            self::deleteDir($file);
        } else {
            unlink($file);
        }
    }
    rmdir($dirPath);
}


function pre($obj,$toDie = true){
    echo '<pre>';
    if(is_array($obj))
            print_r($obj);
    elseif(is_object($obj))
            var_dump($obj);
    else
            echo $obj;		
    echo '</pre>';
    if($toDie) die(' ');
}

function utf8_urldecode($str) {
    $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
    return html_entity_decode($str,null,'UTF-8');;
}

function rearrange( $arr ){
    foreach( $arr as $key => $all ){
        foreach( $all as $i => $val ){
            $new[$i][$key] = $val;    
        }    
    }
    return $new;
}


function curPageURL() {
    $pageURL = 'http://';
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    
    return $pageURL;
}